#include "RenderPch.h"

#include "Val3DRenderer.h"

#include "RenderFacade.h"

#include "ShaderBase.h"

Val3DRenderer::Val3DRenderer(void)
{
}

Val3DRenderer::~Val3DRenderer(void)
{
	SAFE_DELETE( m_shaderManager );
}

void Val3DRenderer::Init()
{
	m_shaderManager = new ShaderManager();
	m_shaderManager->InitShader();
}

void Val3DRenderer::Flush()
{
}

void Val3DRenderer::AddQueueVal3DRendererUnit( Val3DRendererUnit & unit )
{
}

void Val3DRenderer::RenderImmediately( Val3DRendererUnit & unit )
{
	if( RenderFacade::GetInstance() )
	{
		RenderFacade::GetInstance()->SetTransform( D3DTS_WORLD, &unit.m_worldTransform );

		RenderFacade::GetInstance()->SetStreamSource( 0, unit.m_indexedPrimitiveInfo.m_pVB, 0, unit.m_indexedPrimitiveInfo.m_stride );
	
		RenderFacade::GetInstance()->SetIndices( unit.m_indexedPrimitiveInfo.m_pIB );

		if( unit.m_material )
		{
			ShaderBase * shader = m_shaderManager->GetShader( unit.m_material->GetShaderType() );

			int passCount = shader->Begin( unit.m_material, unit.m_indexedPrimitiveInfo.m_fvf );

			for( int cnt = 0; cnt < passCount; cnt++ )
			{
				shader->BeginPass( cnt );

				RenderFacade::GetInstance()->DrawIndexedPrimitive( unit.m_indexedPrimitiveInfo.m_primitiveType, 
																unit.m_indexedPrimitiveInfo.m_baseVertexIndex, 
																unit.m_indexedPrimitiveInfo.m_minIndex, 
																unit.m_indexedPrimitiveInfo.m_numVertices, 
																unit.m_indexedPrimitiveInfo.m_startIndex, 
																unit.m_indexedPrimitiveInfo.m_triangleCount );

				shader->EndPass();
			}

			shader->End();
		}
		else
		{
			RenderFacade::GetInstance()->SetFVF( unit.m_indexedPrimitiveInfo.m_fvf );

			RenderFacade::GetInstance()->DrawIndexedPrimitive( unit.m_indexedPrimitiveInfo.m_primitiveType, 
															unit.m_indexedPrimitiveInfo.m_baseVertexIndex, 
															unit.m_indexedPrimitiveInfo.m_minIndex, 
															unit.m_indexedPrimitiveInfo.m_numVertices, 
															unit.m_indexedPrimitiveInfo.m_startIndex, 
															unit.m_indexedPrimitiveInfo.m_triangleCount );
		}
	}
}